package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.GroupInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.omc.dto.CommissionInfo;
import com.yuke.cloud.service.omc.entity.OdsCommission;
import com.yuke.cloud.service.omc.entity.OdsCommissionOrder;
import com.yuke.cloud.service.omc.service.OdsCommissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* Created by wg on 2019/05/18.
*/
@Api(value = "WEB - OdsCommissionController", tags = "处理OdsCommission相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/commission")
public class OdsCommissionController extends BaseController {
    @Resource
    private OdsCommissionService odsCommissionService;

    @Autowired
    private SmcFeignApi smcFeignApi;

    @Autowired
    private  UacUserFeignApi uacUserFeignApi;

    @PostMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper updateOds(@ApiParam(name = "odsBill", value = "需要修改的内容") @RequestBody OdsCommission odsCommission) {
        odsCommissionService.update(odsCommission);
        return WrapMapper.ok();
    }

    /**
     * 获取云顶商人或云顶店铺累计收益及可提取的收益
     */
    @GetMapping("/getCommissionInfo")
    @ApiOperation(httpMethod = "GET", value = "获取云顶商人或云顶店铺累计收益及可提取的收益")
    public Wrapper<CommissionInfo> getCommissionInfo(@ApiParam(name = "shopId", value = "店铺id") @RequestParam Long shopId) {
        CommissionInfo commissionInfo = odsCommissionService.getCommissionInfo(shopId);
        return WrapMapper.ok(commissionInfo);
    }

    /**
     * 提现申请
     * @param odsCommission
     * @return
     */
    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper<OdsCommission> add(@ApiParam(name = "odsCommission", value = "该数据记录的POJO") @RequestBody OdsCommission odsCommission) {
        // 先查询该店铺是否有在审核或未提现的记录，如果有则返回错误，提示无法再申请
        Example example = new Example(OdsCommission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", odsCommission.getShopId());
        criteria.andIn("checkStatus", Arrays.asList((byte)1, (byte)2));
        if (odsCommissionService.selectCountByExample(example) > 0) {
            return WrapMapper.error("您有未处理完的提现记录，无法再次提交！");
        }


        if (odsCommissionService.saveCommission(odsCommission) == -1) {
            return WrapMapper.error("没有可提现的佣金！");
        }

//        odsCommissionService.save(odsCommission);
        return WrapMapper.ok(odsCommission);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsCommissionService.deleteById(id);
        return WrapMapper.ok();
    }

    /**
     * 该接口用来进行提现审核
     * @param odsCommission
     * @return
     */
    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsCommission", value = "需要修改的内容") @RequestBody OdsCommission odsCommission) {

        odsCommission.setUpdateId(super.getLoginAuthDto().getUserId());
        odsCommission.setUpdateTime(new Date());

        odsCommissionService.updateCommission(odsCommission);
//        odsCommissionService.update(odsCommission);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsCommission> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsCommission odsCommission = odsCommissionService.findById(id);
        return WrapMapper.ok(odsCommission);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsCommission>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsCommission> list = odsCommissionService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 提现操作
     */
    @PostMapping("/withdrawCommission")
    @ApiOperation(httpMethod = "POST", value = "佣金提现操作")
    public Wrapper withdrawCommission(@ApiParam(name = "odsCommission", value = "需要提现单的信息") @RequestBody OdsCommission odsCommission) {

        if (odsCommissionService.withdrawCommission(odsCommission) != 0) {
            return WrapMapper.error("收益提现失败");
        }

        return WrapMapper.ok();
    }

    @PostMapping("/getByCondition")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsCommission>> getByCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                           @ApiParam(name = "odsCommission", value = "检索条件") @RequestBody OdsCommission odsCommission) {
        PageHelper.startPage(page, size);
        Example example = new Example(OdsCommission.class);
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (odsCommission.getCommissionId() != null) {
            criteria.andEqualTo("commissionId", odsCommission.getCommissionId());
        }
        if (odsCommission.getCheckStatus() != null) {
            criteria.andEqualTo("checkStatus", odsCommission.getCheckStatus());
        }
        if (odsCommission.getShopId() != null) {
            criteria.andEqualTo("shopId", odsCommission.getShopId());
        }
        if (odsCommission.getBeginTime() != null && odsCommission.getEndTime() != null) {
            criteria.andLessThanOrEqualTo("createTime", odsCommission.getEndTime());
            criteria.andGreaterThan("createTime", odsCommission.getBeginTime());

        }

        List<OdsCommission> list = odsCommissionService.selectByExample(example);

        Long groupId = getGroupInfoDto().getGroupId();

        Wrapper<GroupInfoDto> groupInfoDto = uacUserFeignApi.getGroupInfoByGroupId(groupId);
        if (groupInfoDto!=null && groupInfoDto.getCode()==200){
            // 登录人为3级部门
            if (groupInfoDto.getResult().getLevel().toString().equals("3")){
                groupId = groupInfoDto.getResult().getParentId();
            }
        }
        List<ShopInfoDto> shopInfoDtos =  smcFeignApi.getShopInfoByGroupId(groupId);

        List<OdsCommission> commissionList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            if (shopInfoDtos != null && shopInfoDtos.size() > 0) {
                for (ShopInfoDto shopInfoDto : shopInfoDtos) {
                    for (OdsCommission temp : list) {
                        if (shopInfoDto.getShopId().longValue() == temp.getShopId().longValue()) {
                            temp.setShopName(shopInfoDto.getShopName());
                            commissionList.add(temp);
                        }
                    }
                }
            }

        }

        PageInfo pageInfo = new PageInfo(commissionList);
        return WrapMapper.ok(pageInfo);
    }
}
